package ch.prokopovi.auxiliary;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ch.prokopovi.db.PureConst;

public class MoneyPointMdCollectorBot extends AbstractCollectorBot {

	private static final String OUT_FILE = "../output_md.sql";
	private static String URL = "http://money.point.md/actions/ExchangeRates/GetOrganisations";
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		System.out.println("--- start ---");

		List<Place> res = new ArrayList<Place>();
		
		String strJson = get(URL);
		JSONArray jsonPlaces = new JSONArray(strJson);
		for (int i = 0; i < jsonPlaces.length(); i++) {
			JSONObject jsonPlace = jsonPlaces.getJSONObject(i);
			
//			"Adress" : "7 Renasterii Avenue, Chisinau, MD-2006, Republic o",
//			"AdressWeb" : "http://www.bnm.org",
//			"Name" : "Banca Nationala a Moldovei",
//			"OrganisationId" : "eaff1a51-31b7-4635-a5b1-8a858f46dbd0",
//			"OrganisationType" : 0,
//			"Phone" : "",
//			"Visible" : true,
//			"BestUsdBuy" : false,
//			"BestUsdSell" : false,
//			"BestEurBuy" : false,
//			"BestEurSell" : false
			
			int id = PureConst.toPositiveHashCode(jsonPlace.getString("OrganisationId"));
			String name = jsonPlace.getString("Name");			
			String addr = jsonPlace.getString("Adress");
			String phone = "+373 22 "+jsonPlace.getString("Phone");
			
			//System.out.println(jsonPlace);
			
			
			Place place = new Place(id, PureConst.CHISINAU_ID, name, null, null, addr, "", phone);
			res.add(place);
			
			//System.out.println(place);
		}	
		
		System.out.printf("%s loaded. ", res.size());

		// sort
		Collections.sort(res, new PlaceIdComparator());
		
		System.out.println();
		
		StringBuilder script = new StringBuilder();
		for (Place place : res) {
			script.append(getInsert(place));
		}
				
		toFile(OUT_FILE, script.toString());
				
		System.out.println("--- end ---");
	}

}
